<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * Includes a formmailer class which provides the feature to send form data as a mail.
 * Extends used PHPMailer in CONTENIDO core.
 *
 * @package     mpContenidoX
 * @subpackage  Form
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: Mailer.class.php 21 2012-07-16 21:33:14Z purcaholic $
 */


defined('CON_FRAMEWORK') or die('Illegal call');


/**
 * @ignore
 */
cInclude('classes', 'class.phpmailer.php');


/**
 * Class to send mail by processing send form data.
 *
 * @package     mpContenidoX
 * @subpackage  Form
 * @author      Murat Purc <murat@purc.de>
 */
class mpForm_Mailer extends PHPMailer {

    /**
     * Form instance
     *
     * @var  mpForm_Base
     */
    protected $_form;


    /**
     * List of fieldnames to put into the generated mail message.
     *
     * @var  array
     */
    protected $_fieldsToExclude = array();


    /**
     * List of fieldnames to exclude from generated mail message.
     *
     * @var  array
     */
    protected $_fieldsToInclude = array();


    public function __construct()
    {
        // donut
    }


    /**
     * A form instance
     *
     * @param  mpForm_Base  $form
     */
    public function setForm(mpForm_Base $form)
    {
        $this->_form = $form;
    }


    /**
     * Set fields to exclude from mail
     *
     * @param  array  $fieldsToExclude
     */
    public function setFieldsToExclude($fieldsToExclude)
    {
        if (is_array($fieldsToExclude)) {
            $this->_fieldsToExclude = $fieldsToExclude;
        }
    }


    /**
     * Set fields to include to mail
     *
     * @param  mpForm_Base  $form
     */
    public function setFieldsToInclude($fieldsToInclude)
    {
        $this->_fieldsToInclude = $fieldsToInclude;
    }


    /**
     * Adds uploaded files as attachement
     *
     * @param  array  $files  Should be superglobal $_FILES
     */
    public function attachUploadedFiles($files)
    {
        if (!is_array($files)) {
            return;
        }

        // add files to mail
        foreach ($files as $pos => $filesItem) {
            if (!empty($filesItem['tmp_name']) && is_file($filesItem['tmp_name'])) {
                $this->AddAttachment($filesItem['tmp_name'], $filesItem['name']);
            }
        }
    }


    /**
     * Renders the mail message.
     *
     * @param   string  $mailcontent  The mail content with at least a wild-cart {content}
     *                                for the form data.
     * @return  string
     */
    public function generateMailMessage($mailcontent = '{content}')
    {
        $msg = '';
        foreach ($this->_getFieldsToProcess() as $name => $item) {
            if ($value = $this->_form->getRequestItem($name)) {
                $msg .= $this->_createMessageItem($name, $value);
            }
        }
        $mailcontent = str_replace('{content}', $msg, $mailcontent);
        return $mailcontent;
    }


    /**
     * Returns the fields which will be used to generate the mail.
     *
     * It checks the properties $_fieldsToInclude and $_fieldsToExclude to collect the field.
     * Property $_fieldsToInclude will be preferred.
     *
     * @param   string  $mailcontent  The mail content with at least a wild-cart {content}
     *                                for the form data.
     * @return  string
     */
    protected function _getFieldsToProcess()
    {
        $elements = $this->_form->getFormElementsForDataView();

        $fields = array();
        if (count($this->_fieldsToInclude) > 0) {
            foreach ($elements as $id => $item) {
                if (in_array($item->getAttribute('name'), $this->_fieldsToInclude)) {
                    $fields[$item->getAttribute('name')] = $item;
                }
            }
        } elseif (count($this->_fieldsToExclude) > 0) {
            foreach ($elements as $id => $item) {
                if (!in_array($item->getAttribute('name'), $this->_fieldsToExclude)) {
                    $fields[$item->getAttribute('name')] = $item;
                }
            }
        }
        return $fields;
    }


    protected function _createMessageItem($label, $value)
    {
        if ($this->ContentType == 'text/html') {
            return $this->_createHtmlMessageItem($label, $value);
        } else {
            return $this->_createTextMessageItem($label, $value);
        }
    }


    protected function _createTextMessageItem($label, $value)
    {
        $msg = '';
        if (strlen($label) > 25 OR strlen($value) > 54) {
            $msg .= "\n" . $label . ":\n\n" . $value . "\n";
        } else {
            $msg .= $label . ":";
            $msg .= str_repeat(' ', 25 - strlen($label));
            $msg .= $value . "\n";
        }
        return $msg;
    }


    protected function _createHtmlMessageItem($label, $value)
    {
        return '<tr><td>' . $label . '</td><td>' . $value . '</td></tr>' . "\n";
    }

}